package com.ouzexi.finalexp

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Looper
import android.util.Log
import android.view.View
import android.widget.Toast
import com.google.gson.Gson
import com.ouzexi.finalexp.util.save
import com.ouzexi.finalexp.vo.registerReturn
import kotlinx.android.synthetic.main.activity_modify_password.*
import okhttp3.*
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONObject
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
import java.io.IOException

class modifyPassword : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_modify_password)
    }

    fun modifyPassword(view: View) {
        val passwordEncoder = BCryptPasswordEncoder()
        val that = this
        // 获取用户输入信息
        val password = modifiedPassword.text
        val cPassword  = yesPassword.text
        val token = save().getData(that,"token")
        if (password.isEmpty() || cPassword.isEmpty()) {
            Looper.prepare()
            Toast.makeText(that,"请输入信息", Toast.LENGTH_SHORT).show()
            Looper.loop()
        } else {
            val urlAPI = "http://10.0.2.2:8080/school/student/updateInfo"
            val json = JSONObject()
            if (password.isNotEmpty()) json.put("password", passwordEncoder.encode(password))
            val requestBody: RequestBody = json.toString().toRequestBody()

            val request = Request.Builder()
            request.url(urlAPI)
            request.addHeader("Content-Type","application/json")
                .addHeader("token",token)
                .post(requestBody)
                .url(urlAPI).build()

            val client = OkHttpClient()

            client.newCall(request.build()).enqueue(object : Callback {

                override fun onResponse(call: Call, response: Response) {
                    val result = response.body?.string()
                    Log.i("testReponse",result.toString())
                    val dataObj: registerReturn = Gson().fromJson(result, registerReturn::class.java)
                    if(dataObj.code==-1) {
                        Log.i("code",dataObj.code.toString())
                        Log.i("test","修改失败")
                        Looper.prepare()
                        Toast.makeText(that,dataObj.msg, Toast.LENGTH_SHORT).show()
                        Looper.loop()
                    }
                    else {
                        Log.i("test","修改成功")
                        // 与数据库匹配
                        Looper.prepare()
                        Toast.makeText(that,"修改成功", Toast.LENGTH_SHORT).show()
                        save().clearData(that,"token")
                        val intent = Intent(that, login::class.java)
                        startActivity(intent)
                        Looper.loop()
                    }
                }

                override fun onFailure(call: Call, e: IOException) {
                    println("Failed request api :( " + e.message)
                    Log.i("testRequest",e.message.toString())
                }

            })
        }
    }
}